package com.richapp.launcher.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.richapp.launcher.common.InfraredInfo;
import com.richapp.launcher.common.JsonResult;
import com.richapp.launcher.database.infrared.dao.BrandDao;
import com.richapp.launcher.database.infrared.dao.BrandModelButtonDao;
import com.richapp.launcher.database.infrared.dao.BrandModelDao;
import com.richapp.launcher.database.infrared.dao.ButtonDao;
import com.richapp.launcher.database.infrared.dao.InfraredVersionDao;
import com.richapp.launcher.database.infrared.dao.MachineTypeDao;
import com.richapp.launcher.database.infrared.mode.Brand;
import com.richapp.launcher.database.infrared.mode.BrandModel;
import com.richapp.launcher.database.infrared.mode.BrandModelButton;
import com.richapp.launcher.database.infrared.mode.Button;
import com.richapp.launcher.database.infrared.mode.InfraredVersion;
import com.richapp.launcher.database.infrared.mode.MachineType;
import com.richapp.launcher.utils.Constants;
import com.richapp.launcher.utils.HttpUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by yif on 2016/10/18.
 */
public class InfraredUpdateService extends Service {

    private static final String TAG = InfraredUpdateService.class.getName();
    //    private static final String UPDATE_URL = Constants.SERVER_BASE_URL + "/infrared/update";
    private static final String UPDATE_URL = "http://192.168.0.100:8080/infrared/update";

    private InfraredVersionDao infraredVersionDao = null;
    private MachineTypeDao machineTypeDao = null;
    private BrandDao brandDao = null;
    private BrandModelDao brandModelDao = null;
    private BrandModelButtonDao brandModelButtonDao = null;
    private ButtonDao buttonDao = null;


    private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        infraredVersionDao = new InfraredVersionDao(this);
        machineTypeDao = new MachineTypeDao(this);
        brandDao = new BrandDao(this);
        brandModelDao = new BrandModelDao(this);
        brandModelButtonDao = new BrandModelButtonDao(this);
        buttonDao = new ButtonDao(this);

        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    update();
                } catch (Throwable throwable) {
                    Log.e(TAG, "更新红外数据库失败", throwable);
                }
            }


        }, 0, 20, TimeUnit.SECONDS);


    }

    private void update() {

        InfraredVersion infraredVersion = infraredVersionDao.getFirst();
        if (infraredVersion == null) {
            infraredVersion = new InfraredVersion();
            infraredVersion.setDbVersion(-1);
            infraredVersionDao.deleteAll();
            infraredVersionDao.add(infraredVersion);
        }
        String result = HttpUtil.get(UPDATE_URL + "?dbVersion=" + infraredVersion.getDbVersion());

        JsonResult<InfraredInfo> jsonResult = JSON.parseObject(result, new TypeReference<JsonResult<InfraredInfo>>() {
        });

        if(jsonResult == null){
            return;
        }

        if ("false".equals(jsonResult.getFlag())) {
            Log.e(TAG, jsonResult.getExp());
            return;
        }

        if (jsonResult.getData() != null) {
            InfraredInfo infraredInfo = jsonResult.getData();

            if (infraredInfo.isNeedUpdate()) {

                List<MachineType> machineTypes = infraredInfo.getMachineTypes();
                List<Button> buttons = infraredInfo.getButtons();
                List<Brand> brands = infraredInfo.getBrands();
                List<BrandModel> brandModels = infraredInfo.getBrandModels();
                List<BrandModelButton> brandModelButtons = infraredInfo.getBrandModelButtons();

                machineTypeDao.deleteAll();
                for (MachineType machineType : machineTypes) {
                    machineTypeDao.add(machineType);
                }

                buttonDao.deleteAll();
                for (Button button : buttons) {
                    buttonDao.add(button);
                }

                brandDao.deleteAll();
                for (Brand brand : brands) {
                    brandDao.add(brand);
                }

                brandModelDao.deleteAll();
                for (BrandModel brandModel : brandModels) {
                    brandModelDao.add(brandModel);
                }

                brandModelButtonDao.deleteAll();
                for (BrandModelButton brandModelButton : brandModelButtons) {
                    brandModelButtonDao.add(brandModelButton);
                }

                InfraredVersion infraredVersion2 = infraredInfo.getInfraredVersion();
                infraredVersionDao.deleteAll();
                infraredVersionDao.add(infraredVersion2);
            }
        }


    }


}
